const App = getApp();
import { CouponModel } from '../../../models/coupon.js'
const ModelCoupon = new CouponModel()

Page({
  data: {
    navs:['未使用','已使用','已失效'],
    winHeight:'',
    currentTab: 0,
    empty:false,
    couponList:null,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false,
    iphoneX: App.globalData.isIphoneX,
  },

  onLoad(options) {
    // Do some initialize when page load.
    const self = this;
    wx.getSystemInfo({
      success: function(res) {
        // console.log(res.windowHeight)
        let clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth
        var calc = clientHeight * rpxR - 410
        // console.log(calc)
        self.setData({
          winHeight: calc
        })
      }
    })
    //获取优惠券
    this.selectMyCoupon();
  },

  onReady() {
    // Do something when page ready.
  },

  onShow() {
    // Do something when page show.
  },

  //查看优惠券规则
  tapToRules: function (event) {
    const ruleType = 17;
    wx.navigateTo({
      url: '/pages/rules/index?ruleType='+ruleType
    })
  },

  //获取我的优惠券
  selectMyCoupon(){
    const self = this;
    const params = {
      couponStatus:self.data.currentTab
    }
    self.setData({
      couponList:null
    })
    ModelCoupon.selectMyCoupon(params).then(res => {
      self.setData({
        couponList:res.data.couponList
      })
    }).catch();
  },

  //顶部切换
  swichNav: function(e) {
    var cur = e.currentTarget.dataset.current
    if (this.data.currentTab == cur) {
      return false
    } else {
      this.setData({
        currentTab: cur
      })
      this.selectMyCoupon();
    }
  },

  //左右滑动
  switchTab: function(e) {
    if(e.detail.source == 'touch'){
      this.setData({
        currentTab: e.detail.current,
        empty: false
      })
      this.selectMyCoupon();
    }
  },

  // 查看详细信息
  checkDetail:function(e){
    const index = e.currentTarget.dataset.index;
    this.data.couponList[index].show = !this.data.couponList[index].show
    this.setData({
      couponList:this.data.couponList
    })
  },

  //扫码
  scan:function(){
    wx.scanCode({
      success(res) {
        wx.navigateTo({
          url: '/pages/account/coupon/addCoupon/index?couponCardNo='+res.result
        })
      },
      fail(){
        // App.WxService.showToast({ title: '扫码失败', icon: 'none' });
      },
      complete(){
        
      }
    })
  },

  

  //兑换优惠券
  tapToAddCoupon:function(){
    wx.navigateTo({
      url: '/pages/account/coupon/addCoupon/index'
    })
    
  },

  onHide() {
    // Do something when page hide.
  },

  onUnload() {
    // Do something when page close.
  },

  onPullDownRefresh() {
    // Do something when pull down.
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  onReachBottom() {
    // Do something when page reach bottom.
  },

  onShareAppMessage() {
    // return custom share data when user share.
  }
})